﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.IO;
using LPV_POS.Module;

namespace LPV_POS.mh_pos.mh_danhmuc
{
    public partial class mh_loaidichvu : DevExpress.XtraEditors.XtraForm
    {
        Mod_messagebox mess;
        public mh_loaidichvu()
        {
            InitializeComponent();
        }
        private void  f_create_control(DataTable tb)
        {
            int x = 24;
            int y = 36;
            int y1 = 0;
            for (int i = 0; i <= tb.Rows.Count - 1; i++)
            {
                CheckBox _control = new CheckBox();
                _control.AutoSize = true;
                _control.Text = tb.Rows[i]["OBJNAME"] + "";
                _control.Tag = tb.Rows[i]["OBJCODE"] + "";
                if (i % 2 == 0)
                    _control.Location = new System.Drawing.Point(x, y + y1);
                else
                {
                    _control.Location = new System.Drawing.Point(x + 175, y + y1);
                    y1 += 41;
                }
                group_dichvu.Controls.Add(_control);
            }
        }

        private void bton_chon_Click(object sender, EventArgs e)
        {
            try
            {
                string _ketqua = "";
                //StreamWriter file_write = null;
                foreach (CheckBox _ctrol in group_dichvu.Controls)
                {
                    if (_ctrol.Checked)
                    {
                        string _str_temp = "#" + _ctrol.Tag.ToString() + "#,";
                        _ketqua += _str_temp;
                    }
                }
                if (_ketqua != "")
                    _ketqua = _ketqua.Substring(0, _ketqua.Length - 1);
                else
                {
                    mess = new Mod_messagebox();
                    mess.messagebox_pos("Thông báo", "Chưa chọn loai dịch vụ", loaithongbao.thongtin.GetHashCode());
                    return;
                }

                _ketqua = new cpublic().MaHoa("", _ketqua);
   
               string sSQL = "UPDATE DM_FUNCTION SET CACDICHVU_12 = '" + _ketqua + "'";
               int _obey  = new thuvien().SP_Update("SP_EXECUTESQL", new string[] { "@sSQL" }, new object[] { sSQL });
               if (_obey > -1)
               {
                   mess = new Mod_messagebox();
                   mess.messagebox_pos("Thông báo", "Chọn thành công", loaithongbao.thongtin.GetHashCode());
                   this.DialogResult = DialogResult.OK;
               }
               else
               {
                   mess = new Mod_messagebox();
                   mess.messagebox_pos("Thông báo", "Không thành công", loaithongbao.thongtin.GetHashCode());
               }

                //if (!File.Exists(Application.StartupPath + @"\function.fsys"))
                //{
                //    file_write = File.CreateText(Application.StartupPath + @"\function.fsys");
                //    file_write.Write(_ketqua);
                //    file_write.Close();
                //}
            
            }
            catch (Exception)
            {
                mess = new Mod_messagebox();
                mess.messagebox_pos("Thông báo", "Không thành công", loaithongbao.thongtin.GetHashCode());
                return;
            }
            finally
            {
                this.Close();
            }
            
        }

        private void mh_loaidichvu_Load(object sender, EventArgs e)
        {
            string sSQL = "SELECT * FROM DM_LOAIDICHVU";
            DataTable tb = new thuvien().SP_SelectDataTable("sp_executesql", new string[] { "@sSQL" }, new object[] { sSQL });
            f_create_control(tb);
        }
    }
}